package mxw.suiye.autopydroid.core.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Message;
import android.view.WindowManager;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;

import mxw.suiye.autopydroid.android.MyApp;
import mxw.suiye.autopydroid.util.DataTool;
import mxw.suiye.autopydroid.util.ResultData;


/**
 * 安卓远程UI管理
 **/
public class Ui {

    public static void toast(String msg) {
        Toast.makeText(MyApp.getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    public static void toast(Message msg) {
        Toast.makeText(MyApp.getContext(),
                ((JSONObject) msg.obj).getJSONObject("data").getString("msg"),
                Toast.LENGTH_SHORT).show();
        if (msg.arg1 != -1) {
            ResultData.reply("0");
        }
    }

    public static void alert(String msg) {
        Message message = new Message();
        message.obj = DataTool.getPayload(2, "", msg);
        dialog(message);
    }

    public static void alert(Message msg) {
        dialog(msg);
    }

    public static void dialog(Message msg) {
        JSONObject data;
        try {
            data = ((JSONObject) msg.obj).getJSONObject("data");
        } catch (Exception e) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("title", "No Title");
            jsonObject.put("msg", "消息格式错误！");
            data = jsonObject;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(MyApp.getContext());
        builder.setTitle(data.getString("title"));
        builder.setMessage(data.getString("msg"));

        builder.setPositiveButton("OK", (dialog, which) -> {
            dialog.dismiss();
            if (msg.arg1 != -1) {
                ResultData.reply("0");
            }
        });
        builder.setOnCancelListener(dialogInterface -> {
            dialogInterface.dismiss();
            if (msg.arg1 != -1) {
                ResultData.reply("-1");
            }
        });

        final android.app.Dialog dialog = builder.create();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        } else {
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        }
        dialog.show();
    }

    public static void confirm(Message msg) {
        JSONObject data;
        try {
            data = ((JSONObject) msg.obj).getJSONObject("data");
        } catch (Exception e) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("title", "No Title");
            jsonObject.put("msg", "消息格式错误！");
            data = jsonObject;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(MyApp.getContext());
        builder.setTitle(data.getString("title"));
        builder.setMessage(data.getString("msg"));
        builder.setCancelable(false);

        builder.setPositiveButton("Yes", (dialog, which) -> {
            dialog.dismiss();
            if (msg.arg1 != -1) {
                ResultData.reply("1");
            }
        });
        builder.setNegativeButton("No", (dialog, which) -> {
            dialog.dismiss();
            if (msg.arg1 != -1) {
                ResultData.reply("0");
            }
        });

        final android.app.Dialog dialog = builder.create();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        } else {
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        }
        dialog.show();
    }

}
